﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static light.http.server.httpserver;


class WSPing
{
    class WSPeer : IWebSocketPeer
    {
        System.Net.WebSockets.WebSocket websocket;
        static int counter = 0;
        public WSPeer(System.Net.WebSockets.WebSocket websocket)
        {
            this.websocket = websocket;
        }
        public async Task OnConnect()
        {
            counter++;
            LocalExt.Logger.OnLog("websocket in. count=" + counter);

            return;
        }

        public async Task OnDisConnect()
        {
            counter--;
            LocalExt.Logger.OnWarn("websocket break. count=" + counter);
            return;
        }

        public async Task OnRecv(MemoryStream stream, int count)
        {
            var buf = new byte[count];
            stream.Read(buf, 0, count);
            await this.websocket.SendAsync(buf, System.Net.WebSockets.WebSocketMessageType.Binary, true
                , System.Threading.CancellationToken.None);
        }
    }
    public static IWebSocketPeer WebSocketCreator(System.Net.WebSockets.WebSocket websocket)
    {
        return new WSPeer(websocket);
    }
}

